package com.yang.service.Impl;

import com.yang.mapper.FoodMapper;
import com.yang.mapper.FoodTypeMapper;
import com.yang.mapper.SysUserMapper;
import com.yang.pojo.Food;
import com.yang.pojo.FoodType;
import com.yang.pojo.SysUser;
import com.yang.service.FoodTypeService;
import com.yang.service.SysUserService;
import com.yang.utils.Constants;
import com.yang.utils.JsonResult;
import com.yang.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by 杨天可 on 2022/3/29 14:18
 */
@Service("foodTypeService")
@Transactional
public class FoodTypeServiceImpl implements FoodTypeService {
    @Resource
    FoodTypeMapper foodTypeMapper;
    @Resource
    FoodMapper foodMapper;
    @Resource
    SysUserMapper sysUserMapper;

    @Override
    public Map<String, Object> save(FoodType foodType) {
        JsonResult jsonResult = new JsonResult();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isEmpty(Objects.toString(foodType.getId(),null))){
            /* 如果id为空，为新增，不为空，为修改*/
            foodTypeMapper.insertSelective(foodType);
        }else {
            foodTypeMapper.updateById(foodType.getId(), foodType.getName(), sdf.format(new Date()));
        }

        jsonResult.setCode(Constants.SUCCESS);
        jsonResult.setMsg("操作成功");

        return jsonResult.getValues();
    }

    @Override
    public FoodType findById(Integer foodTypeId) {
        return foodTypeMapper.findFoodTypeById(foodTypeId);
    }

    @Override
    public Map<String, Object> delFood(Integer foodTypeId) {
        JsonResult jsonResult = new JsonResult();
        List<Food> foods = foodMapper.selectByFoodTypeId(foodTypeId);
        if (foods.size() > 0){
            jsonResult.setCode(Constants.ERROR);
            jsonResult.setMsg("该分类下还存在菜品，不允许删除,请先清理该分类下的菜品");
        }else {
            int i = foodTypeMapper.delFood(foodTypeId);
            if (i > 0) {
                jsonResult.setCode(Constants.SUCCESS);
                jsonResult.setMsg("删除成功");
            } else {
                jsonResult.setCode(Constants.FAIL);
                jsonResult.setMsg("删除失败");
            }
        }
        return jsonResult.getValues();
    }

    @Override
    public Map<String,Object> findAll(Integer userId,Integer currentPage,Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        Integer startPage = (currentPage-1)*pageSize;

        SysUser byId = sysUserMapper.findById(userId);
        if (byId.getUsername().equals("管理员")){//管理员可查看所有数据
            List<FoodType> typeList = foodTypeMapper.findAll();
            List<FoodType> typeLimit = foodTypeMapper.findByLimit(startPage,pageSize);
            jsonResult.setMsg("获取成功");
            jsonResult.setCode(Constants.SUCCESS);
            jsonResult.setData(typeLimit);
            jsonResult.put("total",typeList.size());
        }else {
            List<FoodType> typeLimit = foodTypeMapper.findByUserIdLimit(userId,startPage,pageSize);
            List<FoodType> typeList = foodTypeMapper.findByUserId(userId);
            jsonResult.setMsg("获取成功");
            jsonResult.setCode(Constants.SUCCESS);
            jsonResult.setData(typeLimit);
            jsonResult.put("total",typeList.size());
        }
        return jsonResult.getValues();
    }

    @Override
    public List<FoodType> findAllList() {
        return foodTypeMapper.findAll();
    }

    @Override
    public List<FoodType> findFoodTypeByCurrentUser(Integer userId) {
        SysUser byId = sysUserMapper.findById(userId);
        if (byId.getUsername().equals("管理员")){
            return foodTypeMapper.findAll();
        }else {
            return foodTypeMapper.findByUserId(userId);
        }
    }
}
